package com.wc.alorithm_blue_bridge.线段树.银河纪元;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/12/3 15:22
 * @description https://www.lanqiao.cn/problems/20054/learning/?contest_id=225
 */
public class Main {
    /**
     * x, y, z的值 无论 1， -1的顺序如何, 结果都是一样的
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    // 记录区间内 1 的个数, 懒标记
    static int[] num = new int[N << 2], laz = new int[N << 2];
    static int[] a = new int[N];
    static int n, m, x, y;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        build(1, 1, n);
        m = sc.nextInt();
        while (m-- > 0) {
            int op = sc.nextInt();
            x = sc.nextInt();
            y = sc.nextInt();
            if (op == 1) modify(1, 1, n, x, y);
            else {
                int t = num[1], res = 0;
                if (t >= y) {
                    res += t - y;
                    x += t - y;
                }
                if (n - t >= x) res += n - t - x;
                out.println(res);
            }

        }
        out.flush();
    }

    static void modify(int u, int l, int r, int ql, int qr) {
        if (l == ql && r == qr) {
            laz[u] = -laz[u];
            num[u] = r - l + 1 - num[u];
            return;
        }
        pushDown(u, l, r);
        int mid = l + r >> 1;
        if (qr <= mid) modify(u << 1, l, mid, ql, qr);
        else if (ql > mid) modify(u << 1 | 1, mid + 1, r, ql, qr);
        else {
            modify(u << 1, l, mid, ql, mid);
            modify(u << 1 | 1, mid + 1, r, mid + 1, qr);
        }
        pushUp(u);
    }

    static void build(int u, int l, int r) {
        laz[u] = 1;
        if (l == r) {
            if (a[l] == 1) num[u] = 1;
            return;
        }
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushUp(u);
    }

    static void pushUp(int u) {
        num[u] = num[u << 1] + num[u << 1 | 1];
    }

    static void pushDown(int u, int l, int r) {
        if (laz[u] == -1) {
            laz[u << 1] = laz[u << 1] * laz[u];
            laz[u << 1 | 1] = laz[u << 1 | 1] * laz[u];
            int mid = l + r >> 1;
            num[u << 1] = mid - l + 1 - num[u << 1];
            num[u << 1 | 1] = r - (mid + 1) + 1 - num[u << 1 | 1];
            laz[u] = 1;
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

